//
//  CTEmojiDetailsCollection.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/12.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTEmojiDetailsCollection.h"
#import "CTEmojiDetailsCell.h"
#import "CTHeader.h"
#import "CTDebugManager.h"
#import "CTPublishItems.h"

@interface CTEmojiDetailsCollection()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    UICollectionView *userFeaturesCollectionView;
    CGSize size;
    NSArray * emojiDetailItemArray;
}

@end



@implementation CTEmojiDetailsCollection
@synthesize delegate;
- (void)dealloc
{
    emojiDetailItemArray = nil;
    [userFeaturesCollectionView removeFromSuperview];
    userFeaturesCollectionView = nil;
    delegate = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTEmojiDetailsCollection Is Removed!!!"];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
//        self.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 95);
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.16];
        size = CGSizeMake(95, 95);
        [self createUserCollectionView];
    }
    return self;
}

//创建CollectionView
- (void)createUserCollectionView
{
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    //    flowLayout.headerReferenceSize = CGSizeMake(CT_HEADER_SCREEN_WIDTH, 0);//头部
    userFeaturesCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, CT_HEADER_SCREEN_WIDTH, 95) collectionViewLayout:flowLayout];
    // homePageCollectionView.showsVerticalScrollIndicator = NO;
    userFeaturesCollectionView.delegate = self;
    userFeaturesCollectionView.dataSource = self;
    [self addSubview:userFeaturesCollectionView];
    userFeaturesCollectionView.backgroundColor = [UIColor clearColor];
    [userFeaturesCollectionView registerClass:[CTEmojiDetailsCell class] forCellWithReuseIdentifier:@"cell"];
    [userFeaturesCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView"];
}
#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return emojiDetailItemArray.count;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identify = @"cell";
    
    CTEmojiDetailsCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    [cell sizeToFit];
//    CTEditeEmojiListDetailsItem
//    NSLog(@"class %d",    [[emojiDetailItemArray objectAtIndex:indexPath.row] isKindOfClass:[CTEditeBubbleListItem class]]);
    if ([[emojiDetailItemArray objectAtIndex:indexPath.row] isKindOfClass:[CTEditeBubbleListItem class]])
    {
        CTEditeBubbleListItem *item = [emojiDetailItemArray objectAtIndex:indexPath.row];
        cell.bubbleItem = item;
    }
    else
    {
        CTEditeEmojiListDetailsItem *item = [emojiDetailItemArray objectAtIndex:indexPath.row];
        cell.imgUrl = item.thumb;
    }
    
//    NSLog(@"item.url = %@",item.url);
//    NSLog(@"x = %f y = %f   w = %f  h = %f",[item.x floatValue],[item.y floatValue],[item.w floatValue],[item.h floatValue]);
    //    CTSubjectTimeLineItem * itm = (CTSubjectTimeLineItem *)[subjectArr objectAtIndex:indexPath.row];
    //    cell.imgUrl = itm.thumb;
    //    cell.name = itm.name;
    //    cell.describe = itm.tag_describe;
    //    if ([itm.tag_trends isEqualToString:@"0"])cell.isTrends = NO;
    //    else cell.isTrends = YES;
    //    if ([itm.tag_official isEqualToString:@"0"])cell.isOfficial = NO;
    //    else cell.isOfficial = YES;
    
    return cell;
}

//头部显示的内容
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                            UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView" forIndexPath:indexPath];
    
    //    [headerView addSubview:advertisingColumnView];//头部广告栏
    return headerView;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return size;
    
}
//定义每个UICollectionView 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(1,1,1,1);
}
//定义UICollectionView 每个item的纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 1;
}

//定义每个UICollectionView 纵向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    CTEditeBubbleListItem *item = [emojiDetailItemArray objectAtIndex:indexPath.row];
    if ([[emojiDetailItemArray objectAtIndex:indexPath.row] isKindOfClass:[CTEditeBubbleListItem class]])
    {
        CTEditeBubbleListItem *item = [emojiDetailItemArray objectAtIndex:indexPath.row];
        if([self.delegate respondsToSelector:@selector(selectedEmojiDetailsCollection:withItem:)])
        {
            [self.delegate selectedEmojiDetailsCollection:self withItem:item];
        }
    }
    else
    {
        CTEditeEmojiListDetailsItem *item = [emojiDetailItemArray objectAtIndex:indexPath.row];
        if([self.delegate respondsToSelector:@selector(emojiDetailsCollection:withItem:)])
        {
            [self.delegate emojiDetailsCollection:self withItem:item];
        }
    }

}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark -setter-
- (void)setEmojiDetailsItem:(NSArray *)itemArray
{
    emojiDetailItemArray = itemArray;
    [userFeaturesCollectionView reloadData];
}
@end
